﻿using System;
using System.Globalization;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Isaac.Joy.Identity.Module;
using Isaac.Joy.Identity.Web.DtCms.ExtensionObjects;
using Isaac.Joy.Serialize;
using Newtonsoft.Json;

namespace Isaac.Joy.Identity.Web.DtCms.Controllers
{
    public class LoginController : RepositoryController
    {
        /// <summary>
        /// 用户登录页面
        /// </summary>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        public ActionResult Index(string returnUrl)
        {
            var httpCookie = Response.Cookies[PermissionAttribute.FormsCookieName];
            if (httpCookie != null) httpCookie.Expires = DateTime.Now; if (string.IsNullOrEmpty(returnUrl))
            {
                if (Activator.BundleContext != null)
                {
                    var exts = Activator.BundleContext.GetExtensions("Isaac.Joy.Identity.ReturnUrl");

                    if (exts.Count >= 0)
                    {
                        if (exts[0].Data.Count >= 0)
                        {
                            var returnUrlObj = XmlSerializable.Deserialize<ReturnUrl>(exts[0].Data[0]);
                            returnUrl = returnUrlObj.Value;
                        }
                    }
                }
            }

            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        /// <summary>
        /// 验证用户名密码的Ajax处理方法
        /// </summary>
        /// <param name="loginid"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public ActionResult AuthorizeUser(string loginid, string pwd)
        {
            var user = Repository.AuthorizeUser(loginid, pwd);

            if (user == null) return Content(JsonConvert.SerializeObject(new
            {
                return_code = 0,
                success = false
            }));

            var datenow = DateTime.Now;
            var datexpires = datenow.AddDays(1);
            //获取相关用户信息，保存至cookie中
            //1、生成票据
            var ticket = new FormsAuthenticationTicket(
                1,
                user.Id.ToString(CultureInfo.InvariantCulture),
                datenow,
                datexpires,
                false,
                user.Role.FlagsValue.ToString()
                );
            //2、加密票据并生成Cookie
            var cookie = new HttpCookie(PermissionAttribute.FormsCookieName, FormsAuthentication.Encrypt(ticket))
            {
                Expires = datexpires
            };
            //3、将身份验证票据Cookie输出到客户端
            Response.Cookies.Add(cookie);

            return Content(JsonConvert.SerializeObject(new
            {
                return_code = 0,
                success = true
            }));
        }

        /// <summary>
        /// 登出页面
        /// </summary>
        /// <returns></returns>
        public ActionResult SignOut()
        {
            var httpCookie = Response.Cookies[PermissionAttribute.FormsCookieName];
            if (httpCookie != null) httpCookie.Expires = DateTime.Now;

            return Redirect(FormsAuthentication.LoginUrl);
        }
    }
}
